﻿#region Copyright ©2008-2009, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-05-15 14:42:13 +0200 (Fri, 15 May 2009) $
// Last Changed Revision $Rev: 139 $
//
// ====================================================================================================
#endregion

using System.Collections.Generic;
using System.Xml;
using Tum.CollabXT.VModellXT.Resources;

namespace Tum.CollabXT.VModellXT
{
    public class Product : IProduct
    {
        #region Private Members
        private string _Id;
        private string _Name;
        private string _TypeName;
        private string _Description;
        private Dictionary<string, object> _ExtendedData = new Dictionary<string, object>();

        private bool _IsInitial;
        private bool _IsExternal;
        private bool _HasDocumentTemplate;
        private string _DocumentTemplatePath = string.Empty;
        private string _DisciplineId;
        #endregion

        public Product() { }
        public Product(XmlNode node) 
        {
            try
            {
                _Id = node.Attributes["id"].Value;
                _Name = node.SelectSingleNode("Name").InnerText;
                _TypeName = _Name;
                ExtendedData["DescriptionRaw"] = node.SelectSingleNode("Sinn_und_Zweck").InnerText;

                #region Get text block if availabe
                XmlNode descTextIdNode = node.SelectSingleNode("TextbausteinRef/@link");
                if (descTextIdNode != null)
                {
                    try
                    {
                        ExtendedData["DescriptionRaw"] = node.OwnerDocument.SelectSingleNode(string.Format(Queries.MDL_GetTextBlock, descTextIdNode.Value)).InnerText;
                    }
                    catch { }
                }
                #endregion

                _Description = Helper.StripHtmlString(ExtendedData["DescriptionRaw"] as string);

                _IsInitial = node.Attributes["Initial"].Value.Equals("ja", System.StringComparison.OrdinalIgnoreCase);
                _IsExternal = node.Attributes["Extern"].Value.Equals("ja", System.StringComparison.OrdinalIgnoreCase);

                XmlNode productTemplateNode = node.Attributes["Produktvorlage"];
                if (productTemplateNode != null)
                    _HasDocumentTemplate = node.Attributes["Produktvorlage"].Value.Equals("ja", System.StringComparison.OrdinalIgnoreCase);

                ///TODO: Convert to dependency!
                _DisciplineId = node.SelectSingleNode("DisziplinRef/@link").Value;
            }
            catch { }
        }

        public override string ToString()
        {
            return _Id + " (" + _Name + ")";
        }

        #region IProduct Members

        public bool IsInitial
        {
            get { return (_IsInitial); }
            set { _IsInitial = value; }
        }

        public bool IsExternal
        {
            get { return _IsExternal; }
            set { _IsExternal = value; }
        }

        public bool HasDocumentTemplate
        {
            get { return (_HasDocumentTemplate); }
            set { _HasDocumentTemplate = value; }
        }

        public string DocumentTemplatePath
        {
            get { return (_DocumentTemplatePath); }
            set { _DocumentTemplatePath = value; }
        }

        public string DisciplineId
        {
            get { return (_DisciplineId); }
            set { _DisciplineId = value; }
        }

        public string TypeName
        {
            get { return _TypeName; }
            set { _TypeName = value; }
        }

        #endregion

        #region IProcessElement Members

        public string Id
        {
            get { return (_Id); }
            set { _Id = value; }
        }

        public string Name
        {
            get { return (_Name); }
            set { _Name = value; }
        }

        public string Description
        {
            get { return (_Description); }
            set { _Description = value; }
        }

        public Dictionary<string, object> ExtendedData
        {
            get { return _ExtendedData; }
        }

        #endregion
    }
}
